#include <stdio.h>
#include "TWList.h"


/*链表节点初始化*/
void TWListNode_init(TWListNode *node, void *data) {
    node->nextptr = NULL;
    node->lastptr = NULL;
    node->dataptr = data;
}

/*链表插入节点，把new_node插入到node之后*/
void TWListNode_insert(TWListNode *node, TWListNode *new_node) {

    //把链表后的节点添加到新节点的后面
    if (node->nextptr != NULL) {
        new_node->nextptr = node->nextptr;
    }

    new_node->lastptr = node;
    node->nextptr = new_node;
}

/*删除链表节点, 把node从链表删除*/
void TWListNode_delete(TWListNode *node) {
    if (node->lastptr != NULL) {
        node->lastptr->nextptr = node->nextptr;
    }

    if (node->nextptr != NULL) {
        node->nextptr->lastptr = node->lastptr;
    }
}

/*链表初始化*/
void TWList_init(TWListHead *twlist) {
    twlist->headptr = NULL;
}


/*链表插入节点*/
void TWList_insert_node(TWListHead *twlist, TWListNode *new_node) {
    if (twlist->headptr != NULL) {
        twlist->headptr->lastptr = new_node;
    }
    new_node->lastptr = NULL;
    new_node->nextptr = twlist->headptr;
    twlist->headptr = new_node;
}

/*链表删除节点*/
void TWList_delete_node(TWListHead *twlist, TWListNode *node) {
    if (node->lastptr != NULL) {
        node->lastptr->nextptr = node->nextptr;
    }

    if (node->nextptr != NULL) {
        node->nextptr->lastptr = node->lastptr;
    }

    if (twlist->headptr == node) {
        twlist->headptr = node->nextptr;
    }
}

//判断列表为空
int TWList_is_empty(TWListHead *twlist) {
    if (twlist->headptr == NULL)
        return 1;
    return 0;
}